﻿using System;
using System.Threading.Tasks;

namespace WPF_TY_UI
{
    public static class BusyExtend
    {
        public static async Task<TResult> Run<TResult>(this TYWindow t, Func<TResult> function, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                //await Task.Delay(2000);
                return await Task.Run<TResult>(function);
            }
        }

        public static async Task<TResult> Run<TResult>(this TYWindow t, Func<TResult> function, int delayTime, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                await Task.Delay(delayTime);
                return await Task.Run<TResult>(function);
            }
        }

        public static async Task Run(this TYWindow t, Action function, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                await Task.Run(function);
            }
        }

        public static async Task Run(this TYWindow t, Action function, int delayTime, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                await Task.Delay(delayTime);
                await Task.Run(function);
            }
        }

        public static void SetBusy(this TYWindow t, bool isBusy, string title = "请稍等..")
        {
            t.BusyTitle = title;
            t.IsBusy = isBusy;
        }

        public static async Task<TResult> RunEx<TResult>(this TYUserControl t, Func<TResult> function, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                //await Task.Delay(2000);
                return await Task.Run<TResult>(function);
            }
        }

        public static async Task<TResult> RunEx<TResult>(this TYUserControl t, Func<TResult> function, int delayTime, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                await Task.Delay(delayTime);
                return await Task.Run<TResult>(function);
            }
        }

        public static async Task RunEx(this TYUserControl t, Action function, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                await Task.Run(function);
            }
        }

        public static async Task RunEx(this TYUserControl t, Action function, int delayTime, string busyTitle = "请稍等...")
        {
            using (var busy = new BusyHelper(t, busyTitle))
            {
                await Task.Delay(delayTime);
                await Task.Run(function);
            }
        }

        public static void SetBusy(this TYUserControl t, bool isBusy, string title = "请稍等..")
        {
            t.BusyTitle = title;
            t.IsBusy = isBusy;
        }
    }
}
